package team2.game.crow;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ScoreDB extends SQLiteOpenHelper {
	
	// ===========================================================
    // Constants
    // ===========================================================
	private static final int DATABASE_VERSION = 1;
	private static final String SCORES_TABLE_NAME = "High_Scores";
	private static final String SCORES_COLUMN_NAME = "Name";
	private static final String SCORES_COLUMN_SCORE = "Score";
    private static final String SCORES_TABLE_CREATE = "CREATE TABLE " + SCORES_TABLE_NAME + " (" + SCORES_COLUMN_NAME + " TEXT, " + SCORES_COLUMN_SCORE + " INTEGER);";
    
    private static final String[] SCORES_PROJECTION = new String[] {
    	SCORES_COLUMN_NAME,
    	SCORES_COLUMN_SCORE,
    };
	// ===========================================================
    // Fields
    // ===========================================================
    
	// ===========================================================
    // Constructors
    // ===========================================================
	
	public ScoreDB(Context context) {
		super(context, SCORES_TABLE_NAME, null, DATABASE_VERSION);
	}
	
    // ===========================================================
    // Getter & Setter
    // ===========================================================
	
	public String[] getScores(){
		SQLiteDatabase db = this.getReadableDatabase();
		
		Cursor c = db.query(SCORES_TABLE_NAME, SCORES_PROJECTION, null, null, null, null, SCORES_COLUMN_SCORE + " DESC");
		
		String[] scores = new String[c.getCount()];
		
		c.moveToFirst();
		for(int x = 0; x < c.getCount(); x++){
			scores[x] = c.getString(0) +" :"+c.getInt(1);
			c.moveToNext();
		}
		
		return scores;
	}
	
    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
	
	@Override
	public void onCreate(SQLiteDatabase db){
		db.execSQL(SCORES_TABLE_CREATE);
		ContentValues values = new ContentValues();
		values.put(SCORES_COLUMN_NAME, "Odin");
		values.put(SCORES_COLUMN_SCORE, 10000);
		db.insert(SCORES_TABLE_NAME, null, values);
		values.clear();
		values.put(SCORES_COLUMN_NAME, "Loki");
		values.put(SCORES_COLUMN_SCORE, 12000);
		db.insert(SCORES_TABLE_NAME, null, values);
		values.clear();
		values.put(SCORES_COLUMN_NAME, "JC");
		values.put(SCORES_COLUMN_SCORE, 8000);
		db.insert(SCORES_TABLE_NAME, null, values);
		values.clear();
		values.put(SCORES_COLUMN_NAME, "Calvin");
		values.put(SCORES_COLUMN_SCORE, 6000);
		db.insert(SCORES_TABLE_NAME, null, values);
		values.clear();
		values.put(SCORES_COLUMN_NAME, "Gaston");
		values.put(SCORES_COLUMN_SCORE, 4000);
		db.insert(SCORES_TABLE_NAME, null, values);
		values.clear();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}
	
	// ===========================================================
    // Methods
    // ===========================================================
	
	public void addScore(String name, int score){
		SQLiteDatabase db = this.getReadableDatabase();		
		/*
		Cursor c = db.query(SCORES_TABLE_NAME, SCORES_PROJECTION, null, null, null, null, SCORES_COLUMN_SCORE + " ASC");
		c.moveToFirst();
		c.getPosition();
		
		db.delete(SCORES_TABLE_NAME, "", whereArgs)
		*/
		ContentValues values = new ContentValues();
		values.put(SCORES_COLUMN_NAME, name);
		values.put(SCORES_COLUMN_SCORE, score);
		db.insert(SCORES_TABLE_NAME, null, values);
		
	}
	
	// ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================

}